package cn.iocoder.yudao.module.resource.controller.admin.timepackage;

import cn.iocoder.yudao.module.resource.controller.admin.timepackage.vo.TimePackagePageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.timepackage.vo.TimePackageRespVO;
import cn.iocoder.yudao.module.resource.controller.admin.timepackage.vo.TimePackageSaveReqVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.timepackage.TimePackageDO;
import cn.iocoder.yudao.module.resource.service.timepackage.TimePackageService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 时长套餐")
@RestController
@RequestMapping("/lm/time-package")
@Validated
public class TimePackageController {

    @Resource
    private TimePackageService timePackageService;

    @PostMapping("/create")
    @Operation(summary = "创建时长套餐")
    @PreAuthorize("@ss.hasPermission('lm:time-package:create')")
    public CommonResult<String> createTimePackage(@Valid @RequestBody TimePackageSaveReqVO createReqVO) {
        return success(timePackageService.createTimePackage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新时长套餐")
    @PreAuthorize("@ss.hasPermission('lm:time-package:update')")
    public CommonResult<Boolean> updateTimePackage(@Valid @RequestBody TimePackageSaveReqVO updateReqVO) {
        timePackageService.updateTimePackage(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得时长套餐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('lm:time-package:query')")
    public CommonResult<TimePackageRespVO> getTimePackage(@RequestParam("id") String id) {
        TimePackageDO timePackage = timePackageService.getTimePackage(id);
        return success(BeanUtils.toBean(timePackage, TimePackageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得时长套餐分页")
    @PreAuthorize("@ss.hasPermission('lm:time-package:query')")
    public CommonResult<PageResult<TimePackageRespVO>> getTimePackagePage(@Valid TimePackagePageReqVO pageReqVO) {
        PageResult<TimePackageDO> pageResult = timePackageService.getTimePackagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TimePackageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出时长套餐 Excel")
    @PreAuthorize("@ss.hasPermission('lm:time-package:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTimePackageExcel(@Valid TimePackagePageReqVO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TimePackageDO> list = timePackageService.getTimePackagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "时长套餐.xls", "数据", TimePackageRespVO.class,
                BeanUtils.toBean(list, TimePackageRespVO.class));
    }

}